package com.diandian.backend.store.service;

import com.diandian.backend.store.constants.DianDianStoreConstant;
import com.diandian.backend.store.domain.StoreElemeRetailStoreInfo;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.mapper.ElemeRetailStoreMapper;
import com.diandian.backend.store.resp.*;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ElemeRetailStoreService {

    @Autowired
    private ElemeRetailStoreMapper elemeRetailStoreMapper;

    @Autowired
    private StoreElemeRetailStoreInfoService elemeRetailShangouStoreInfoService;

    @Autowired
    private StoreElemeRetailStoreTreeService elemeRetailShangouStoreTreeService;

    @Autowired
    private StoreElemeRetailStoreInfoTreeService elemeRetailShangouStoreInfoTreeService;

    public PageResult storePage(StoreElemeRetailStorePageParamDTO dataDTO){

        dataDTO.check();
        int pageNum = dataDTO.getPageNum();
        int pageSize = dataDTO.getPageSize();
        String nodeCode = dataDTO.getNodeCodeEql();

        // 校验节点信息
        String nodePath = "";
        // 非顶级节点、非未分组节点
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(nodeCode) && !DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)){
            StoreElemeRetailStoreTreeDetailParamDTO treeDetailParamDTO = new StoreElemeRetailStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(dataDTO.getNodeCodeEql());
            treeDetailParamDTO.setBelong(dataDTO.getBelongEql());
            treeDetailParamDTO.setOrgId(dataDTO.getOrgIdEql());
            treeDetailParamDTO.setOrgCode(dataDTO.getOrgCodeEql());
            treeDetailParamDTO.setDel(0);
            StoreElemeRetailStoreTreeDetailResp treeDetailResp = elemeRetailShangouStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("当前节点不合法");
            }
            nodePath = treeDetailResp.getNodePath();
        }
        // 未分组节点
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)) {
            nodePath = DianDianStoreConstant.TREE_NODE_CODE_UNGROUP;
        }

        // 分页查询所有当前节点下的门店数据
        int totalCount = elemeRetailStoreMapper.getStoreCount(nodePath, dataDTO);
        List<StoreElemeRetailStoreInfo> dataList = elemeRetailStoreMapper.getStorePage(nodePath, dataDTO, pageSize, (pageNum - 1) * pageSize);

        // 组装数据
        List<StoreElemeRetailStoreInfoPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){

            // 组装门店编码和节点编码集合
            StoreElemeRetailStoreInfoTreeListParamDTO listParamDTO = new StoreElemeRetailStoreInfoTreeListParamDTO();
            listParamDTO.setBelongEql(dataDTO.getBelongEql());
            listParamDTO.setOrgIdEql(dataDTO.getOrgIdEql());
            listParamDTO.setOrgCodeEql(dataDTO.getOrgCodeEql());
            List<StoreElemeRetailStoreInfoTreeListResp> relationList = elemeRetailShangouStoreInfoTreeService.getList(listParamDTO);
            Map<String, String> storeCodeAndNodeCodeMap = new HashMap<>();
            if(relationList != null && relationList.size() > 0){
                for (int i = 0; i < relationList.size(); i++) {
                    storeCodeAndNodeCodeMap.put(relationList.get(i).getStoreCode(), relationList.get(i).getNodeCode());
                }
            }

            // 组装节点编码和节点名称集合
            StoreElemeRetailStoreTreeListParamDTO treeListParamDTO = new StoreElemeRetailStoreTreeListParamDTO();
            treeListParamDTO.setBelongEql(dataDTO.getBelongEql());
            treeListParamDTO.setOrgIdEql(dataDTO.getOrgIdEql());
            treeListParamDTO.setOrgCodeEql(dataDTO.getOrgCodeEql());
            List<StoreElemeRetailStoreTreeListResp> treeListRespList = elemeRetailShangouStoreTreeService.getList(treeListParamDTO);
            Map<String, String> treeCodeAndNameMap = new HashMap<>();
            if(treeListRespList != null && treeListRespList.size() > 0){
                for (int i = 0; i < treeListRespList.size(); i++) {
                    treeCodeAndNameMap.put(treeListRespList.get(i).getNodeCode(), treeListRespList.get(i).getNodeName());
                }
            }

            // 组装返回结构
            dataList.stream().forEach( x ->{
                StoreElemeRetailStoreInfoPageResp item = new StoreElemeRetailStoreInfoPageResp();
                item.setId(x.getId());
                item.setOfflineCode(x.getOfflineCode());
                item.setOnlineCode(x.getOnlineCode());
                item.setOnlineName(x.getOnlineName());
                item.setBelong(x.getBelong());
                item.setRemark(x.getRemark());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                item.setName(x.getName());
                item.setShopLogo(x.getShopLogo());
                item.setProvince(x.getProvince());
                item.setCity(x.getCity());
                item.setCounty(x.getCounty());
                item.setAddress(x.getAddress());
                item.setPhone(x.getPhone());
                item.setIvrPhone(x.getIvrPhone());
                item.setServicePhone(x.getServicePhone());
                item.setServicePhones(x.getServicePhones());
                item.setLongitude(x.getLongitude());
                item.setLatitude(x.getLatitude());
                item.setCoordType(x.getCoordType());
                item.setInvoiceSupport(x.getInvoiceSupport());
                item.setPackageBoxPrice(x.getPackageBoxPrice());
                item.setBusinessFormId(x.getBusinessFormId());
                item.setCategorys(x.getCategorys());
                item.setBusinessTime2(x.getBusinessTime2());
                item.setBizIdentity(x.getBizIdentity());
                item.setDeliveryTimeType(x.getDeliveryTimeType());
                item.setImData(x.getImData());
                item.setStatus(x.getStatus());
                if(storeCodeAndNodeCodeMap.get(x.getOnlineCode()) == null){
                    item.setNodeCode(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP);
                    item.setNodeName(DianDianStoreConstant.TREE_NODE_NAME_UNGROUP);
                } else {
                    String code = storeCodeAndNodeCodeMap.get(x.getOnlineCode());
                    item.setNodeCode(code);
                    item.setNodeName(treeCodeAndNameMap.get(code));
                }
                respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public StoreElemeRetailStoreInfoDetailResp getStoreDetail(ElemeRetailStoreDetailParamDTO param) {

        String belong = param.getBelongEql();

        StoreElemeRetailStoreInfoDetailParamDTO storeInfoDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeInfoDetailParamDTO.setId(param.getId());
        storeInfoDetailParamDTO.setOnlineCode(param.getCode());
        storeInfoDetailParamDTO.setBelong(belong);
        storeInfoDetailParamDTO.setOrgId(param.getOrgIdEql());
        storeInfoDetailParamDTO.setOrgCode(param.getOrgCodeEql());
        storeInfoDetailParamDTO.setDel(0);
        StoreElemeRetailStoreInfoDetailResp detailResp = elemeRetailShangouStoreInfoService.getDetail(storeInfoDetailParamDTO);
        if(detailResp == null){
            return null;
        }
        String storeCode = detailResp.getOnlineCode();
        String orgId = detailResp.getOrgId();
        String orgCode = detailResp.getOrgCode();

        StoreElemeRetailStoreInfoTreeDetailResp relationDTO = elemeRetailShangouStoreInfoTreeService.getNodeCodeByStoreCode(belong, orgId, orgCode, storeCode);
        detailResp.setNodeCode((relationDTO == null || relationDTO.getNodeCode() == null) ? "" : relationDTO.getNodeCode());
        return detailResp;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addStore(ElemeRetailStoreAddParamDTO dataDTO){

        String belong = dataDTO.getBelong();
        String nodeCode = dataDTO.getNodeCode();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String onlineStoreCode = dataDTO.getOnlineCode();

        // 校验节点是否合法：如果是未分组节点则设置为空
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(nodeCode) && !DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)){
            StoreElemeRetailStoreTreeDetailParamDTO treeDetailParamDTO = new StoreElemeRetailStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(nodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreElemeRetailStoreTreeDetailResp treeDetailResp = elemeRetailShangouStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("当前节点不合法");
            }
        } else if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(nodeCode)) {
            nodeCode = null;
        }

        // 校验线上门店编码是否存在
        StoreElemeRetailStoreInfoDetailParamDTO storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setOnlineCode(onlineStoreCode);
        storeDetailParamDTO.setDel(0);
        StoreElemeRetailStoreInfoDetailResp storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp != null){
            throw new DefaultException("已存在此门店编码");
        }

        // 校验线下门店编码是否已经绑定其他线上门店编码
        storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setOfflineCode(dataDTO.getOfflineCode());
        storeDetailParamDTO.setDel(0);
        storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp != null && storeInfoDetailResp.getOnlineCode() != null && !storeInfoDetailResp.getOnlineCode().equals(onlineStoreCode)){
            throw new DefaultException("线下门店已绑定门店编码为" + storeInfoDetailResp.getOnlineCode() + "的线上门店");
        }

        // 保存门店信息
        StoreElemeRetailStoreInfoAddParamDTO storeInfoAddParamDTO = new StoreElemeRetailStoreInfoAddParamDTO();
        storeInfoAddParamDTO.setOfflineCode(dataDTO.getOfflineCode());
        storeInfoAddParamDTO.setOnlineCode(dataDTO.getOnlineCode());
        storeInfoAddParamDTO.setOnlineName(dataDTO.getOnlineName());
        storeInfoAddParamDTO.setBelong(dataDTO.getBelong());
        storeInfoAddParamDTO.setRemark(dataDTO.getRemark());
        storeInfoAddParamDTO.setCreateUserId(dataDTO.getCreateUserId());
        storeInfoAddParamDTO.setCreateUserName(dataDTO.getCreateUserName());
        storeInfoAddParamDTO.setCreateTime(dataDTO.getCreateTime());
        storeInfoAddParamDTO.setOrgId(dataDTO.getOrgId());
        storeInfoAddParamDTO.setOrgCode(dataDTO.getOrgCode());
        storeInfoAddParamDTO.setName(dataDTO.getName());
        storeInfoAddParamDTO.setShopLogo(dataDTO.getShopLogo());
        storeInfoAddParamDTO.setProvince(dataDTO.getProvince());
        storeInfoAddParamDTO.setCity(dataDTO.getCity());
        storeInfoAddParamDTO.setCounty(dataDTO.getCounty());
        storeInfoAddParamDTO.setAddress(dataDTO.getAddress());
        storeInfoAddParamDTO.setPhone(dataDTO.getPhone());
        storeInfoAddParamDTO.setIvrPhone(dataDTO.getIvrPhone());
        storeInfoAddParamDTO.setServicePhone(dataDTO.getServicePhone());
        storeInfoAddParamDTO.setServicePhones(dataDTO.getServicePhones());
        storeInfoAddParamDTO.setLongitude(dataDTO.getLongitude());
        storeInfoAddParamDTO.setLatitude(dataDTO.getLatitude());
        storeInfoAddParamDTO.setCoordType(dataDTO.getCoordType());
        storeInfoAddParamDTO.setInvoiceSupport(dataDTO.getInvoiceSupport());
        storeInfoAddParamDTO.setPackageBoxPrice(dataDTO.getPackageBoxPrice());
        storeInfoAddParamDTO.setBusinessFormId(dataDTO.getBusinessFormId());
        storeInfoAddParamDTO.setCategorys(dataDTO.getCategorys());
        storeInfoAddParamDTO.setBusinessTime2(dataDTO.getBusinessTime2());
        storeInfoAddParamDTO.setBizIdentity(dataDTO.getBizIdentity());
        storeInfoAddParamDTO.setDeliveryTimeType(dataDTO.getDeliveryTimeType());
        storeInfoAddParamDTO.setImData(dataDTO.getImData());
        storeInfoAddParamDTO.setStatus(dataDTO.getStatus());
        storeInfoAddParamDTO.setCreateUserId(dataDTO.getCreateUserId());
        storeInfoAddParamDTO.setCreateUserName(dataDTO.getCreateUserName());
        storeInfoAddParamDTO.setOrgId(orgId);
        storeInfoAddParamDTO.setOrgCode(orgCode);
        storeInfoAddParamDTO.setBelong(belong);
        elemeRetailShangouStoreInfoService.add(storeInfoAddParamDTO);

        // 保存门店和节点的关联关系
        if(nodeCode != null){
            StoreElemeRetailStoreInfoTreeAddParamDTO storeInfoTreeAddParamDTO = new StoreElemeRetailStoreInfoTreeAddParamDTO();
            storeInfoTreeAddParamDTO.setNodeCode(nodeCode);
            storeInfoTreeAddParamDTO.setStoreCode(onlineStoreCode);
            storeInfoTreeAddParamDTO.setRemark(dataDTO.getRemark());
            storeInfoTreeAddParamDTO.setBelong(belong);
            storeInfoTreeAddParamDTO.setOrgId(orgId);
            storeInfoTreeAddParamDTO.setOrgCode(orgCode);
            storeInfoTreeAddParamDTO.setCreateUserId(dataDTO.getCreateUserId());
            storeInfoTreeAddParamDTO.setCreateUserName(dataDTO.getCreateUserName());
            elemeRetailShangouStoreInfoTreeService.add(storeInfoTreeAddParamDTO);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeById(Long id, String belong){

        // 校验门店是否存在
        StoreElemeRetailStoreInfoDetailParamDTO storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setId(id);
        storeDetailParamDTO.setBelong(belong);
        StoreElemeRetailStoreInfoDetailResp storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }
        Long storeDataId = storeInfoDetailResp.getId();
        String orgId = storeInfoDetailResp.getOrgId();
        String orgCode = storeInfoDetailResp.getOrgCode();
        String nodeCode = storeInfoDetailResp.getNodeCode();
        String storeCode = storeInfoDetailResp.getOnlineCode();

        // todo 检查是否门店已被引用
        boolean isUsedFlag = false;
        if(isUsedFlag){
            throw new DefaultException("已引用的门店，不能被删除");
        }

        // 查询门店所在节点
        StoreElemeRetailStoreInfoTreeDetailParamDTO relationDTO = new StoreElemeRetailStoreInfoTreeDetailParamDTO();
        relationDTO.setBelong(belong);
        relationDTO.setStoreCode(storeCode);
        StoreElemeRetailStoreInfoTreeDetailResp relationResp = elemeRetailShangouStoreInfoTreeService.getDetail(relationDTO);
        if(relationResp != null){
            nodeCode = relationResp.getNodeCode();
        }

        // 删除门店
        StoreElemeRetailStoreInfoDeleteByIdParamDTO deleteByIdParamDTO = new StoreElemeRetailStoreInfoDeleteByIdParamDTO();
        deleteByIdParamDTO.setId(storeDataId);
        elemeRetailShangouStoreInfoService.deleteById(deleteByIdParamDTO);

        // 删除关联关系
        elemeRetailShangouStoreInfoTreeService.remove(belong, orgId, orgCode, nodeCode, storeCode);
    }

    public void removeStore(ElemeRetailStoreRemoveParamDTO dataDTO){

        dataDTO.check();

        Long id = dataDTO.getId();
        String belong = dataDTO.getBelong();
        String nodeCode = dataDTO.getNodeCode();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String storeCode = dataDTO.getStoreCode();

        // 校验节点是否合法
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(nodeCode)){
            StoreElemeRetailStoreTreeDetailParamDTO treeDetailParamDTO = new StoreElemeRetailStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(nodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreElemeRetailStoreTreeDetailResp treeDetailResp = elemeRetailShangouStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("节点不合法");
            }
        }

        // 校验门店是否存在
        StoreElemeRetailStoreInfoDetailParamDTO storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setId(id);
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setOnlineCode(storeCode);
        storeDetailParamDTO.setDel(0);
        StoreElemeRetailStoreInfoDetailResp storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }
        Long storeDataId = storeInfoDetailResp.getId();

        // 校验此门店是否在此节点下
        StoreElemeRetailStoreInfoTreeDetailParamDTO storeInfoTreeDetailParamDTO = new StoreElemeRetailStoreInfoTreeDetailParamDTO();
        storeInfoTreeDetailParamDTO.setBelong(belong);
        storeInfoTreeDetailParamDTO.setOrgId(orgId);
        storeInfoTreeDetailParamDTO.setOrgCode(orgCode);
        storeInfoTreeDetailParamDTO.setStoreCode(storeCode);
        storeInfoTreeDetailParamDTO.setNodeCode(nodeCode);
        storeInfoTreeDetailParamDTO.setDel(0);
        StoreElemeRetailStoreInfoTreeDetailResp storeElemeRetailStoreInfoTreeDetailResp = elemeRetailShangouStoreInfoTreeService.getDetail(storeInfoTreeDetailParamDTO);
        if(storeElemeRetailStoreInfoTreeDetailResp == null){
            throw new DefaultException("节点下无此门店");
        }

        // todo 检查是否门店已被引用
        boolean isUsedFlag = false;
        if(isUsedFlag){
            throw new DefaultException("已引用的门店，不能被删除");
        }

        // 删除门店
        StoreElemeRetailStoreInfoDeleteByIdParamDTO deleteByIdParamDTO = new StoreElemeRetailStoreInfoDeleteByIdParamDTO();
        deleteByIdParamDTO.setId(storeDataId);
        elemeRetailShangouStoreInfoService.deleteById(deleteByIdParamDTO);

        // 删除关联关系
        elemeRetailShangouStoreInfoTreeService.remove(belong, orgId, orgCode, nodeCode, storeCode);
    }

    public void updateStoreBasicInfo(ElemeRetailStoreBasicUpdateParamDTO dataDTO){

        dataDTO.check();
        String belong = dataDTO.getBelong();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();
        String onlineStoreCode = dataDTO.getOnlineCode();

        // 校验门店编码是否存在
        StoreElemeRetailStoreInfoDetailParamDTO storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setOnlineCode(onlineStoreCode);
        storeDetailParamDTO.setDel(0);
        StoreElemeRetailStoreInfoDetailResp storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }
        Long id = storeInfoDetailResp.getId();

        // 校验线下门店编码是否已经绑定其他线上门店编码
        storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setOfflineCode(dataDTO.getOfflineCode());
        storeDetailParamDTO.setDel(0);
        storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp != null && storeInfoDetailResp.getOnlineCode() != null && !storeInfoDetailResp.getOnlineCode().equals(onlineStoreCode)){
            throw new DefaultException("线下门店已绑定门店编码为" + storeInfoDetailResp.getOnlineCode() + "的线上门店");
        }

        // 更新门店信息
        StoreElemeRetailStoreInfoUpdateByIdParamDTO storeBasicInfoDTO = new StoreElemeRetailStoreInfoUpdateByIdParamDTO();
        storeBasicInfoDTO.setId(id);
        storeBasicInfoDTO.setOfflineCode(dataDTO.getOfflineCode());
//        storeBasicInfoDTO.setOnlineCode(dataDTO.getOnlineCode());
        storeBasicInfoDTO.setOnlineName(dataDTO.getOnlineName());
        storeBasicInfoDTO.setBelong(dataDTO.getBelong());
        storeBasicInfoDTO.setRemark(dataDTO.getRemark());
        storeBasicInfoDTO.setOrgId(dataDTO.getOrgId());
        storeBasicInfoDTO.setOrgCode(dataDTO.getOrgCode());
        storeBasicInfoDTO.setUpdateUserId(dataDTO.getUpdateUserId());
        storeBasicInfoDTO.setUpdateUserName(dataDTO.getUpdateUserName());
        storeBasicInfoDTO.setUpdateTime(dataDTO.getUpdateTime());
        storeBasicInfoDTO.setName(dataDTO.getName());
        storeBasicInfoDTO.setShopLogo(dataDTO.getShopLogo());
        storeBasicInfoDTO.setProvince(dataDTO.getProvince());
        storeBasicInfoDTO.setCity(dataDTO.getCity());
        storeBasicInfoDTO.setCounty(dataDTO.getCounty());
        storeBasicInfoDTO.setAddress(dataDTO.getAddress());
        storeBasicInfoDTO.setPhone(dataDTO.getPhone());
        storeBasicInfoDTO.setIvrPhone(dataDTO.getIvrPhone());
        storeBasicInfoDTO.setServicePhone(dataDTO.getServicePhone());
        storeBasicInfoDTO.setServicePhones(dataDTO.getServicePhones());
        storeBasicInfoDTO.setLongitude(dataDTO.getLongitude());
        storeBasicInfoDTO.setLatitude(dataDTO.getLatitude());
        storeBasicInfoDTO.setCoordType(dataDTO.getCoordType());
        storeBasicInfoDTO.setInvoiceSupport(dataDTO.getInvoiceSupport());
        storeBasicInfoDTO.setPackageBoxPrice(dataDTO.getPackageBoxPrice());
        storeBasicInfoDTO.setBusinessFormId(dataDTO.getBusinessFormId());
        storeBasicInfoDTO.setCategorys(dataDTO.getCategorys());
        storeBasicInfoDTO.setBusinessTime2(dataDTO.getBusinessTime2());
        storeBasicInfoDTO.setBizIdentity(dataDTO.getBizIdentity());
        storeBasicInfoDTO.setDeliveryTimeType(dataDTO.getDeliveryTimeType());
        storeBasicInfoDTO.setImData(dataDTO.getImData());
        storeBasicInfoDTO.setStatus(dataDTO.getStatus());
        storeBasicInfoDTO.setUpdateUserId(dataDTO.getUpdateUserId());
        storeBasicInfoDTO.setUpdateUserName(dataDTO.getUpdateUserName());
        storeBasicInfoDTO.setUpdateTime(dataDTO.getUpdateTime() == null ? new Date() : dataDTO.getUpdateTime());
        elemeRetailShangouStoreInfoService.updateBasicInfo(storeBasicInfoDTO);
    }

    public void updateStoreNodeInfo(ElemeRetailStoreNodeUpdateParamDTO dataDTO){

        String storeCode = dataDTO.getCode();
        String sourceNodeCode = dataDTO.getSourceNodeCode();
        String targetNodeCode = dataDTO.getTargetNodeCode();
        String belong = dataDTO.getBelong();
        String orgId = dataDTO.getOrgId();
        String orgCode = dataDTO.getOrgCode();

        // 源节点和目标节点一致，则无需处理
        if(sourceNodeCode.equals(targetNodeCode)){
            return;
        }

        // 跟节点下不能有门店
        if(DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetNodeCode)){
            throw new DefaultException(DianDianStoreConstant.TREE_NODE_NAME_TOP + "下不能关联门店");
        }

        // 校验门店编码是否存在
        StoreElemeRetailStoreInfoDetailParamDTO storeDetailParamDTO = new StoreElemeRetailStoreInfoDetailParamDTO();
        storeDetailParamDTO.setBelong(belong);
        storeDetailParamDTO.setOrgId(orgId);
        storeDetailParamDTO.setOrgCode(orgCode);
        storeDetailParamDTO.setOnlineCode(storeCode);
        storeDetailParamDTO.setDel(0);
        StoreElemeRetailStoreInfoDetailResp storeInfoDetailResp = elemeRetailShangouStoreInfoService.getDetail(storeDetailParamDTO);
        if(storeInfoDetailResp == null){
            throw new DefaultException("门店不存在");
        }

        // 1. 从“未分组”移动到“其他节点”时
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(sourceNodeCode)){

            // 校验目标节点是否合法
            if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetNodeCode)){
                StoreElemeRetailStoreTreeDetailParamDTO treeDetailParamDTO = new StoreElemeRetailStoreTreeDetailParamDTO();
                treeDetailParamDTO.setNodeCode(targetNodeCode);
                treeDetailParamDTO.setBelong(belong);
                treeDetailParamDTO.setOrgId(orgId);
                treeDetailParamDTO.setOrgCode(orgCode);
                treeDetailParamDTO.setDel(0);
                StoreElemeRetailStoreTreeDetailResp treeDetailResp = elemeRetailShangouStoreTreeService.getDetail(treeDetailParamDTO);
                if(treeDetailResp == null){
                    throw new DefaultException("目标节点不合法");
                }
            }

            // 删除可能存在的关联关系，避免出现重复关联数据
            elemeRetailShangouStoreInfoTreeService.deleteByStoreCode(belong, orgId, orgCode, storeCode);

            // 添加关联关系
            StoreElemeRetailStoreInfoTreeAddParamDTO addParamDTO = new StoreElemeRetailStoreInfoTreeAddParamDTO();
            addParamDTO.setNodeCode(targetNodeCode);
            addParamDTO.setBelong(belong);
            addParamDTO.setOrgId(orgId);
            addParamDTO.setOrgCode(orgCode);
            addParamDTO.setStoreCode(storeCode);
            addParamDTO.setCreateUserId(dataDTO.getUpdateUserId());
            addParamDTO.setCreateUserName(dataDTO.getUpdateUserName());
            elemeRetailShangouStoreInfoTreeService.add(addParamDTO);

            return;

        }

        // 2. 如果从“其他节点”迁移到“未分组”时
        if(DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(targetNodeCode)){

            // 校验原节点是否有此门店
            Long id = null;
            if(!DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(sourceNodeCode)){
                StoreElemeRetailStoreInfoTreeDetailParamDTO infoTreeDetailParamDTO = new StoreElemeRetailStoreInfoTreeDetailParamDTO();
                infoTreeDetailParamDTO.setNodeCode(sourceNodeCode);
                infoTreeDetailParamDTO.setStoreCode(storeCode);
                infoTreeDetailParamDTO.setBelong(belong);
                infoTreeDetailParamDTO.setOrgId(orgId);
                infoTreeDetailParamDTO.setOrgCode(orgCode);
                infoTreeDetailParamDTO.setDel(0);
                StoreElemeRetailStoreInfoTreeDetailResp sourceRelationResp = elemeRetailShangouStoreInfoTreeService.getDetail(infoTreeDetailParamDTO);
                if(sourceRelationResp == null){
                    throw new DefaultException("原节点下无此门店");
                }
                id = sourceRelationResp.getId();
            }

            // 删除关联关系
            StoreElemeRetailStoreInfoTreeDeleteByIdParamDTO deleteByIdParamDTO = new StoreElemeRetailStoreInfoTreeDeleteByIdParamDTO();
            deleteByIdParamDTO.setId(id);
            elemeRetailShangouStoreInfoTreeService.deleteById(deleteByIdParamDTO);

            return;
        }

        // 3. 如果节点迁移，不涉及到“未分组节点”
        // 校验目标节点是否合法
        if(!DianDianStoreConstant.TREE_NODE_CODE_TOP.equals(targetNodeCode)){
            StoreElemeRetailStoreTreeDetailParamDTO treeDetailParamDTO = new StoreElemeRetailStoreTreeDetailParamDTO();
            treeDetailParamDTO.setNodeCode(targetNodeCode);
            treeDetailParamDTO.setBelong(belong);
            treeDetailParamDTO.setOrgId(orgId);
            treeDetailParamDTO.setOrgCode(orgCode);
            treeDetailParamDTO.setDel(0);
            StoreElemeRetailStoreTreeDetailResp treeDetailResp = elemeRetailShangouStoreTreeService.getDetail(treeDetailParamDTO);
            if(treeDetailResp == null){
                throw new DefaultException("目标节点不合法");
            }
        }

        // 校验原节点是否有此门店（从“未分组”移动到“其他节点”时不校验）
        Long id = null;
        if(!DianDianStoreConstant.TREE_NODE_CODE_UNGROUP.equals(sourceNodeCode)){
            StoreElemeRetailStoreInfoTreeDetailParamDTO infoTreeDetailParamDTO = new StoreElemeRetailStoreInfoTreeDetailParamDTO();
            infoTreeDetailParamDTO.setNodeCode(sourceNodeCode);
            infoTreeDetailParamDTO.setStoreCode(storeCode);
            infoTreeDetailParamDTO.setBelong(belong);
            infoTreeDetailParamDTO.setOrgId(orgId);
            infoTreeDetailParamDTO.setOrgCode(orgCode);
            infoTreeDetailParamDTO.setDel(0);
            StoreElemeRetailStoreInfoTreeDetailResp sourceRelationResp = elemeRetailShangouStoreInfoTreeService.getDetail(infoTreeDetailParamDTO);
            if(sourceRelationResp == null){
                throw new DefaultException("原节点下无此门店");
            }
            id = sourceRelationResp.getId();
        }

        // 如果节点迁移，不涉及到“未分组节点”，修改门店节点关联关系
        StoreElemeRetailStoreInfoTreeUpdateByIdParamDTO updateDTO = new StoreElemeRetailStoreInfoTreeUpdateByIdParamDTO();
        updateDTO.setId(id);
        updateDTO.setNodeCode(targetNodeCode);
        updateDTO.setUpdateUserId(dataDTO.getUpdateUserId());
        updateDTO.setUpdateUserName(dataDTO.getUpdateUserName());
        updateDTO.setUpdateTime(new Date());
        elemeRetailShangouStoreInfoTreeService.updateRelation(updateDTO);
    }
}
